//
//  SEPatientFileViewController.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/17.
//

import UIKit
import DNSPageView

class SEPatientFileViewController: SEBaseViewController {

    var patientViewModel = SEPatientDetailViewModel()

    init(with viewModel: SEPatientDetailViewModel) {
        self.patientViewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        title = "档案详情"
        super.viewDidLoad()
    }
    
    override func setupUI() {
        super.setupUI()

        view.backgroundColor = UIColor.white
        
        view.addSubview(pageViewManager.titleView)
        pageViewManager.titleView.setShadow()
        pageViewManager.titleView.snp.makeConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.left.right.equalToSuperview()
            make.top.equalTo(self.fileInfoHeader.snp.bottom)
            make.height.equalTo(50)
        }
        
        view.addSubview(pageViewManager.contentView)
        pageViewManager.contentView.snp.makeConstraints { (maker) in
            maker.top.equalTo(pageViewManager.titleView.snp.bottom)
            maker.left.right.bottom.equalToSuperview()
        }
    }

    @IBOutlet private weak var fileInfoHeader: SEPatientInfoView! {
        didSet {
            fileInfoHeader.view.setLayerCorners(rect: fileInfoHeader.bounds, radius: 14, corners: [.topLeft, .topRight])
            fileInfoHeader.view.layer.borderWidth = 1
            fileInfoHeader.view.layer.borderColor = UIColor(hex: "#CFE7F7").withAlphaComponent(0.8).cgColor
            fileInfoHeader.view.layer.masksToBounds = true
            fileInfoHeader.updateView(patientViewModel)
            fileInfoHeader.editClosure = {
                self.pushToPatientDetail()
            }
        }
    }
    
    private lazy var pageViewManager: PageViewManager = {
        let style = PageStyle()
        style.isShowBottomLine = true
        style.isTitleScaleEnabled = true
        style.isTitleViewScrollEnabled = true
        style.titleFont = UIFont.regular(14)
        style.titleColor = Asset.textDark.color
        style.titleSelectedColor = Asset.main.color
        style.bottomLineColor = Asset.main.color
        style.bottomLineWidth = 50
        style.bottomLineRadius = 2
        
        let titles = ["基础档案", "健康档案"]

//        let titles = ["基础档案"]

        for i in 0..<titles.count {
            if i == 0 {
                let vc = SEPatientFileBasicViewController(with: patientViewModel)
                addChild(vc)
            }
            if i == 1 {
                let vc = SEPatientFileHealthViewController(with: patientViewModel)
                addChild(vc)
            }
        }

        return PageViewManager(style: style, titles: titles, childViewControllers: children)
    }()

}

extension SEPatientFileViewController {
    private func pushToPatientDetail() {
        let vc = SEPatientDetailViewController(with: patientViewModel, editType: .edit)
        navigationController?.pushViewController(vc, animated: true)
    }
}
